<?php

/**
 * Index controller class
 *
 */

class ContactsController extends Client_Controller
{
	public function init()
    {
        parent::init();
        $this->setActiveCat(337);
    }

 public function indexAction()
    {


        $form = new Llib_Form($this->_request);
    	$data = array();

    	if ($this->_request->isPost())
    	{
	    	  $data = $form->GetPostedData();
	    	  $ok = true;

	    	  if(trim($data['name']) == ''){
					 $form->SetError("Необходимо ввести Имя");
					 $ok = false;
	    	  }

    		  /* if(trim($data['company']) == '' && $ok){
					 $form->SetError("Необходимо ввести название компании");
					 $ok = false;
	    	  }

    		   if(trim($data['position']) == '' && $ok){
					 $form->SetError("Необходимо ввести Должность");
					 $ok = false;
	    	  }

    		  if(trim($data['phone']) == '' && $ok){
					 $form->SetError("Необходимо ввести Контактный телефон");
					 $ok = false;
	    	  }*/

	    	  if(!($data['email'] = $this->check_email($data['email']))  && $ok){
					 $form->SetError($this->_ERROR);
					 $ok = false;
	    	  }

    		 if(trim($data['text']) == '' && $ok){
					 $form->SetError("Необходимо ввести Текст сообщения");
					 $ok = false;
	    	  }

    	  	  if($ok)
	    	  {
	    	  	 $this->SendMail($data);
	    	 	 $form->SetSuccess(($this->_lang == 'ru' ? "Сообщение успешно отправлено" : "Message sent successfully"));
	    	 	 $data = array();
	    	  }
    	}

    	$form->AddData($data);

    	$this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	         $this->view->assign($form->GetFrom());
		    $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }

    //************************************************/
    //  Mailing
    //************************************************/

	public function SendMail($data)
	{
    	$phone = Zend_Registry::get('config')->contacts->phone;
    	$email = Zend_Registry::get('config')->contacts->email;
    	$email_cc = Zend_Registry::get('config')->contacts->email_cc;
    	$title = Zend_Registry::get('config')->title;
    	$domain = Zend_Registry::get('config')->domains->front;


		$mail = new Zend_Mail("Windows-1251");
		$text = '<p>Имя: <strong>'.$data['name'].'</strong></p>';
		$text .= '<p>Email: <strong>'.$data['email'].'</strong></p>';
		//$text .= '<p>Название компании: <strong>'.$data['company'].'</strong></p>';
		//$text .= '<p>Должность: <strong>'.$data['position'].'</strong></p>';
		$text .= '<p>Контактный телефон: <strong>'.$data['phone'].'</strong></p>';
		$text .= '<p>Текст: '.$data['text'].'</p>';


    	$mail->setBodyHtml(iconv("UTF-8", "Windows-1251", $text), 'Windows-1251');

    	$mail->setFrom($email, $title);
    	$mail->addTo($email);
    	$mail->addCc($email_cc);
    	$mail->setSubject(iconv("UTF-8", "Windows-1251", '['.$domain.'] -Обратная связь!'));
    	$mail->send();
	}

    public function check_email($val)
    {
		$val = trim($val);
        if($val == ''){
        	$this->_ERROR = 'Необходимо ввести E-mail';
        	return false;
        }

    	$vald_email = new Zend_Validate_EmailAddress();
        if(!$vald_email->isValid($val))
        {
           $this->_ERROR = implode(',', $vald_email->getMessages());
           return false;
        }

		return $val;
    }


}
